package com.pets.springboot_petshop.controller;

import com.pets.springboot_petshop.entity.Product;
import com.pets.springboot_petshop.pojo.Result;
import com.pets.springboot_petshop.service.PettypeService;
import com.pets.springboot_petshop.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private PettypeService pettypeService;

    @GetMapping("/selectid")
    public Result selectid(@RequestParam Integer id) {
        Product product = productService.selectid(id);
        if (product == null) {
            return Result.error("获取失败");
        } else {
            return Result.success(product);
        }
    }

    @GetMapping("/selectpettype_id")
    public Result selectpettype_id(@RequestParam String pettype_id) {
        List<Product> products = productService.selectpettype_id(pettype_id);
        if (products == null || products.isEmpty()) {
            return Result.error("没有找到相关产品");
        } else {
            return Result.success(products);
        }
    }

    @GetMapping("/selectname")
    public Result selectname(@RequestParam String name) {
        List<Product> products = productService.selectname(name);
        if (products == null || products.isEmpty()) {
            return Result.error("获取失败");
        } else {
            return Result.success(products);
        }
    }
    @GetMapping("/selectstate")
    public Result selectstate(@RequestParam String state) {
        List<Product> products = productService.selectstate(state);
        if (products == null || products.isEmpty()) {
            return Result.error("获取失败");
        } else {
            return Result.success(products);
        }
    }
    @GetMapping("/selectrecommend")
    public Result selectrecommend(@RequestParam String recommend) {
        List<Product> products = productService.selectrecommend(recommend);
        if (products == null || products.isEmpty()) {
            return Result.error("获取失败");
        } else {
            return Result.success(products);
        }
    }

    @GetMapping("/findAll")
    public Result findAll() {
        List<Product> products = productService.findAll();
        if (products == null || products.isEmpty()) {
            return Result.error("获取失败");
        } else {
            return Result.success(products);
        }
    }

    @PostMapping("/add")
    public Result add(@RequestBody Product product) {
        try {
            int result = productService.add(product);
            if (result > 0) {
                return Result.success("新增成功");
            } else {
                return Result.error("新增失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务异常：" + e.toString());
        }
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        if (productService.delete(id) > 0) {
            return Result.success();
        } else {
            return Result.error("删除失败");
        }
    }

    @PostMapping("/update")
    public Result update(@RequestBody Product product) {
        try {
            if (product == null) {
                return Result.error("商品信息不能为空");
            }

            int updateResult = productService.update(product);
            if (updateResult > 0) {
                return Result.success("修改成功");
            } else {
                return Result.error("修改失败");
            }
        } catch (Exception e) {
            return Result.error("服务更新异常：" + e.toString());
        }
    }
//    @PostMapping("/update")
//    public Result update(@RequestBody Product product) {
//
//    }
@PostMapping("/batchUpdateStatus")
public Result batchUpdateStatus(@RequestBody Map<String, Object> payload) {
    // 从payload中获取orderIds和status
    List<Integer> orderIdList = (List<Integer>) payload.get("ids");
    String newState = (String) payload.get("state");
    // 将List转换为int数组
    int[] ids = new int[orderIdList.size()];
    for (int i = 0; i < orderIdList.size(); i++) {
        ids[i] = orderIdList.get(i);
    }

    // 调用service层方法执行批量更新
    int updated = productService.batchUpdateStatus(ids, newState);
    return updated > 0 ? Result.success("批量更新状态成功") : Result.error("批量更新状态失败");
}
    @PostMapping("/batchUpdateRecommend")
    public Result batchUpdateRecommend(@RequestBody Map<String, Object> payload) {
        // 从payload中获取orderIds和status
        List<Integer> orderIdList = (List<Integer>) payload.get("ids");
        String recommend = (String) payload.get("recommend");
        // 将List转换为int数组
        int[] ids = new int[orderIdList.size()];
        for (int i = 0; i < orderIdList.size(); i++) {
            ids[i] = orderIdList.get(i);
        }

        // 调用service层方法执行批量更新
        int updated = productService.batchUpdateRecommend(ids, recommend);
        return updated > 0 ? Result.success("批量更新状态成功") : Result.error("批量更新状态失败");
    }
    @PostMapping("/admindelete")
    public Result admindelete(@RequestBody Map<String, Object> payload) {
        try {
            // 从payload中获取orderIds
            List<Integer> orderIdList = (List<Integer>) payload.get("ids");
            if (orderIdList == null || orderIdList.isEmpty()) {
                return Result.error("未提供有效的ID列表");
            }

            // 将List转换为int数组
            int[] ids = new int[orderIdList.size()];
            for (int i = 0; i < orderIdList.size(); i++) {
                ids[i] = orderIdList.get(i);
            }

            int affectedRows = productService.admindelete(ids);
            if (affectedRows > 0) {
                return Result.success("删除成功");
            } else {
                return Result.error("删除失败");
            }
        } catch (ClassCastException e) {
            return Result.error("ID列表格式错误，应为整数数组");
        } catch (Exception e) {
            return Result.error("服务器错误：" + e.getMessage());
        }
    }
}